package repository

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"gorm.io/gorm"
)

type SysLogininforRepository struct {
	DB *gorm.DB
}

func NewSysLogininforRepository() *SysLogininforRepository {
	return &SysLogininforRepository{
		DB: db.GetMasterDB(),
	}
}
func (r *SysLogininforRepository) GetById(infoId int64) (*model.SysLogininfor, error) {
	db := r.DB
	m := &model.SysLogininfor{}
	err := db.Model(&model.SysLogininfor{}).Where("info_id = ?", infoId).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *SysLogininforRepository) List(query *vo.QuerySysLogininforReq, pageReq *request.PageRequest) ([]*model.SysLogininfor, int64, error) {
	tx := r.DB.Model(&model.SysLogininfor{})
	if query != nil {
		if query.InfoId != nil {
			tx = tx.Where("info_id = ?", query.InfoId)
		}
		if query.UserName != nil {
			tx = tx.Where("user_name like ?", "%"+*query.UserName+"%")
		}
		if query.Ipaddr != nil {
			tx = tx.Where("ipaddr like ?", "%"+*query.Ipaddr+"%")
		}
		if query.Status != nil {
			tx = tx.Where("status = ?", *query.Status)
		}
		if query.Params != nil {
			val, have := query.Params["beginTime"]
			if have {
				beginTime := val.(string)
				tx = tx.Where("date_format(login_time,'%Y%m%d') >= date_format('" + beginTime + "','%Y%m%d')")
			}
			val, have = query.Params["endTime"]
			if have {
				endTime := val.(string)
				tx = tx.Where("date_format(login_time,'%Y%m%d') <= date_format('" + endTime + "','%Y%m%d')")
			}
		}
	}
	list := make([]*model.SysLogininfor, 0)
	if pageReq != nil {
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			tx = tx.Select("info_id, user_name, ipaddr, login_location, browser, os, status, msg, login_time")
			tx = db.BuildPageAndOrder(tx, &query.PageRequest, "info_id desc")
			if err := tx.Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		tx = tx.Select("info_id, user_name, ipaddr, login_location, browser, os, status, msg, login_time")
		if err := tx.Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *SysLogininforRepository) Save(m *model.SysLogininfor) error {
	return r.DB.Model(&model.SysLogininfor{}).Create(m).Error
}

// Update 更新
func (r *SysLogininforRepository) Update(m *model.SysLogininfor) error {
	return r.DB.Model(&model.SysLogininfor{}).Where("info_id = ?", m.InfoId).Updates(m).Error
}

// Delete 删除
func (r *SysLogininforRepository) Delete(infoId int64) error {
	return r.DB.Model(&model.SysLogininfor{}).Where("info_id = ?", infoId).Delete(&model.SysLogininfor{}).Error
}

// DeleteBatch 批量删除
func (r *SysLogininforRepository) DeleteBatch(ids []string) error {
	db := r.DB
	db = db.Where("info_id in ?", ids)
	return db.Delete(&model.SysLogininfor{}).Error
}
func (r *SysLogininforRepository) Clean() error {
	return r.DB.Exec("truncate table sys_logininfor").Error
}
